<?php
namespace app\api\controller\company;

use app\api\controller\Baseuser;
use app\common\model\company\Company as Companymodel;
use app\common\model\company\CompanyTouch as CompanyTouchmodel;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\customer\Subscribe;
use think\facade\Env;
use app\common\model\power\Rolepower;
use think\File;

class Companytouch extends Baseuser
{
    /**
     * 产生jwt加密串，参考session
     */
    public function initialize()
    {
        parent::initialize();
        // self::is_login();
        // Rolepower::powerguard(201102);
    }

    /**
     * 获取产废单位排查清单列表
     * @return [type] [description]
     */
    public function getlist(){
        $data = input('get.');
        $data['Type'] = 'CompanyTouch';
        $data['order'] = 'TouchTime desc';
        $data = init_where($data);
        if (Rolepower::ispower(204007)) {
            //管理员权限
            //直接通过无需筛选
        }else if (Rolepower::ispower(201003)) {
            //业务员
            $data['where'][] = ['CreateUserId',$this->user_id];
        }else  if (Rolepower::ispower(101006)) {
            //企业负责人
            $company = Company::user_getinfo();
            $data['where'][] = ['CompanyId',$company['CompanyId']];
        } else {
            return moerr('权限不足，无法查看信息','goback');
        }
        $list = CompanyTouchmodel::usergetlist($data);
        return myok($list); 
        
    }

    /**
     * 获取创建前的数据
     * @return [type] [description]
     */
    public function getcreateinitinfo()
    {
        $id = input('id');
        if (empty($id)) {
            return myerr('请选择企业','','goback');
        }
        $company = Company::get($id);
        if(empty($company)){
            return myerr('没找到您需要的企业','','goback');
        }
        $user = UserModel::get($this->user_id);
        $data['company'] = ['Name'=>$company['Name'],'UserTel'=>empty($company['UserTel'])?$company['Tel']:$company['UserTel'],'UserName'=>$company['UserName']];
        $data['buss']    = ['Name'=>$user['Name'],'UserTel'=>$user['Iphone']];
        return myok($data);
    }

    /**
     * 获取数据
     * @return [type] [description]
     */
    public function getinfo()
    {
        $id = input('id');
        if (empty($id)) {
            return myerr('缺少参数','','goback');
        }
        $data = CompanyTouchmodel::user_getinfo($id);
        return myok($data);
    }

    /**
     * 删除此摸排单
     * @return [type] [description]
     */
    public function deleteinfo(){
        $id = input('id');
        if (empty($id)) {
            return myerr('缺少参数','','goback');
        }
        $data = CompanyTouchmodel::deleteinfo($id);
        return myok($data);
    }
    /**
     * 修改整改信息
     * @return [type] [description]
     */
    public function update_zhenggai(){
        $idata = input('post.');
        if (empty($idata['id'])) {
            return myerr('没找到您需要修改的信息');
        }
        $data = CompanyTouchmodel::get($idata['id']);
        if (empty($data)) {
            return moerr('没有找到您需要的信息');
        }
        //权限验证
        $company = Company::get($data['CompanyId']);
        if ($data['CreateUserId'] == $this->user_id) {
            //创建排查清单 通过验证
        } else if (!$company->guard_state || $company->BussinessId == $this->user_id) {
            //客户负责人 通过验证
        } else if (Rolepower::ispower(204007)) {
            //管理员权限
        } else if ($company->UserId == $this->user_id) {
            //企业负责人权限
        } else {
            return moerr('权限不足，无法修改此客户整改信息');
        }
        $list = ['ZhengGaiImg','ZhengGaiTime','ZhengGaiBeiZhu'];
        $change = false;
        foreach ($list as $key => $value) {
            if(isset($idata[$value])){
                $data[$value] = $idata[$value];
                $change = true;
            }
        }
        if($change){
            $data->ZhengGaiUpdateTime  = date('Y-m-d H:i:s',time());
            $data->ZhengGaiUpUserId = $this->user_id;
            $data->save();
        }
        return myok();
    }

    /**
     * 修改摸排单
     * @return [type] [description]
     */
    public function reinfo()
    {
        $idata = input('post.');
        if (empty($idata)) {
            return myerr('缺少需要的信息');
        }
        if (empty($idata['id'])) {
            return myerr('没找到您需要修改的信息');
        }
        $ndata['ToucheId'] = $idata['id'];
        $filed              = ['UserName', 'UserTel', 'TouchTime', 'ZhuYaoQingKuang', 'WuRanXSQK', 'WuRanZGYJ', 'WuRanBZ', 'TaiZhangZhuCunXSQK', 'TaiZhangZhuCunZGYJ', 'TaiZhangZhuCunBZ', 'BiaoShiXSQK', 'BiaoShiZGYJ', 'BiaoShiBZ', 'WeiFeiXSQK', 'WeiFeiZGYJ', 'WeiFeiBZ', 'TaiZhangXSQK', 'TaiZhangZGYJ', 'TaiZhangBZ', 'ZhuCunXSQK', 'ZhuCunZGYJ', 'ZhuCunBZ', 'TouchUserName', 'TouchUserTel','WuRanImg','TaiZhangZhuCunImg','BiaoShiImg','WeiFeiImg','TaiZhangImg','ZhuCunImg'];
        foreach ($filed as $key => $value) {
            if (isset($idata[$value])) {
                $ndata[$value] = trim($idata[$value]);
            }
        }
        if(!empty($idata['list'])){
            $ndata['list'] = json_array($idata['list'],true);
        }
        //添加排查清单
        $customerid = CompanyTouchmodel::user_re($ndata);
        //删除预约
        Subscribe::where('CompanyTouchId',$customerid)->delete();
        //添加预约
        if(!empty($idata['SubscribeTime'])){
            $idata['CompanyId'] = $idata['CompanyId'];
            $idata['CompanyTouchId'] = $customerid;
            $idata['Content'] = $idata['Note'];
            $idata['u_id'] = $this->user_id;
            $subscribe = new Subscribe;
            $subscribe->addSubscribe($idata);
        }
        return myok($customerid);
    }


    /**
     * 新增摸排信息
     * @return [type] [description]
     */
    public function createinfo()
    {
        $idata = input('post.');
        if (empty($idata)) {
            return myerr('缺少需要的信息');
        }
        if (empty($idata['id'])) {
            return myerr('请选择企业');
        }
        $ndata['CompanyId'] = $idata['id'];
        $filed              = ['UserName', 'UserTel', 'TouchTime', 'ZhuYaoQingKuang', 'WuRanXSQK', 'WuRanZGYJ', 'WuRanBZ', 'TaiZhangZhuCunXSQK', 'TaiZhangZhuCunZGYJ', 'TaiZhangZhuCunBZ', 'BiaoShiXSQK', 'BiaoShiZGYJ', 'BiaoShiBZ', 'WeiFeiXSQK', 'WeiFeiZGYJ', 'WeiFeiBZ', 'TaiZhangXSQK', 'TaiZhangZGYJ', 'TaiZhangBZ', 'ZhuCunXSQK', 'ZhuCunZGYJ', 'ZhuCunBZ','TouchUserName', 'TouchUserTel','WuRanImg','TaiZhangZhuCunImg','BiaoShiImg','WeiFeiImg','TaiZhangImg','ZhuCunImg'];
        foreach ($filed as $key => $value) {
            if (!empty($idata[$value])) {
                $ndata[$value] = trim($idata[$value]);
            }
        }
        if(!empty($idata['list'])){
            $ndata['list'] = json_array($idata['list'],true);
        }
        //添加排查清单
        $customerid = CompanyTouchmodel::user_create($ndata);
        if(!empty($idata['SubscribeTime'])){
            $idata['CompanyId'] = $idata['id'];
            $idata['CompanyTouchId'] = $customerid;
            $idata['Content'] = $idata['Note'];
            $idata['u_id'] = $this->user_id;
            $subscribe = new Subscribe;
            $subscribe->addSubscribe($idata);
        }
        return myok($customerid);
    }

    /**
     * 图片上传
     * @return [type] [description]
     */
    public function upimg()
    {
        $type = 'touch';
        try {
            $file = request()->file('img');
        } catch (\Exception $e) {
            return json_encode(['status' => 'error', 'data' => $e->getMessage()]);
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        if (!is_null($file)) {
            try {
                $save_path = Env::get('root_path') . 'public/static' . '/upimg/customer/' . $type . '/';
                $info      = $file->validate(["size" => 50000000, 'ext' => 'jpg,png,gif'])->rule("date")->move($save_path);
                //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
                $image = \think\Image::open($save_path . '/' . $info->getSaveName());
                $image->thumb(2000, 2000)->save($save_path . '/' . $info->getSaveName());
                if ($info) {
//getSaveName();
                    $p = $info->getFilename();
                    return json_encode(['status' => 'ok', 'data' => SERVERURL . 'static/upimg/customer/' . $type . '/' . date('Ymd', time()) . '/' . $p]);
                } else {
                    return json_encode(['status' => 'error', 'data' => $info->getError()]);
                }
            } catch (\Exception $e) {
                return json_encode(['status' => 'error', 'data' => $e->getMessage()]);
            }

        } else {
            return json_encode(['status' => 'error', 'data' => '没有找到需要上传的图片']);
        }
    }

    /**
     * 判断账号是否登录
     * @return boolean [description]
     */
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
}
